// server-middleware/decrypt.js
const CryptoJS = require("crypto-js");
export default function (req, res) {
  const KEY_PWD = process.env.DES_KEY;
  const CIPHER_ALGORITHM = "DES-CBC";
  // 提取前端发送的被加密的文本
  const cipherText = req.body.cipherText;
  console.log(cipherText)
  try {
    let keyBytes = CryptoJS.enc.Utf8.parse(
      KEY_PWD.padEnd(CIPHER_ALGORITHM === "DES-CBC" ? 8 : 16)
    );
    let ivBytes = CryptoJS.enc.Hex.parse(process.env.DES_IV);
    let decrypted = CryptoJS.DES.decrypt(
      { ciphertext: CryptoJS.enc.Base64.parse(cipherText) },
      keyBytes,
      ivBytes
    );
    console.log(keyBytes,'keyBytes');
    console.log(ivBytes,'ivBytes');
    let decryptedText = decrypted.toString(CryptoJS.enc.Utf8);
    // res.statusCode = 200;
    // res.setHeader("Content-Type", "application/json");
    // res.end(JSON.stringify({ decryptedText }));
  } catch (e) {
    console.error(e);
    res.statusCode = 500;
    res.end(JSON.stringify({ error: "Decryption failed" }));
  }
}
